/**
 * @作者 Wang Sicheng
 * @时间 2019年7月20日14:58:36
 */
package com.chinaunicom.tour.sys.controller;

import java.util.Date;
import java.util.List;

import com.chinaunicom.tour.sys.entity.StragegyCollect;
import com.chinaunicom.tour.sys.model.StragegyCollectModel;
import com.chinaunicom.tour.sys.service.StragegyCollectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.chinaunicom.tour.sys.constant.CommonConstants;
import com.chinaunicom.tour.sys.constant.ResultConstants;
import com.chinaunicom.tour.sys.entity.Stragegy;
import com.chinaunicom.tour.sys.entity.User;
import com.chinaunicom.tour.sys.model.StragegyModel;
import com.chinaunicom.tour.sys.service.StragegyService;
import com.chinaunicom.tour.sys.util.ObjectRestResponse;
import com.chinaunicom.tour.sys.util.TableResultResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tk.mybatis.mapper.genid.GenId;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "攻略管理", tags = { "攻略管理" })
@RestController
@RequestMapping("stragegy")
public class StragegyController {

    @Autowired
    StragegyService stragegyService;
    @Autowired
    StragegyCollectService stragegyCollectService;
    @ApiOperation(value = "新增", notes = "")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Stragegy> add(@RequestBody @Validated Stragegy entity,BindingResult results,HttpServletRequest request) {
        ObjectRestResponse<Stragegy> orr = new ObjectRestResponse<Stragegy>();
        orr.setData(null);
        try {
        	
        	////////////////////此处获取用户ID，获取不到则是未登录////////////////
            User user;
            HttpSession session = request.getSession();


            /////////////////////临时模拟登录////////////////////////////
//            User usertemp=new User();
//            usertemp.setId("1");
//            session.setAttribute("user",usertemp);
            /////////////////////////!!!!!!!!!!//////////////////////////


            user=(User)session.getAttribute("user");
            String userid=null;
            if(!(user==null)){
                userid=user.getId();
            }
            if (userid ==null || userid.equals("")) {
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("用户未登录");
                return orr;
            }
            entity.setUserId(userid);
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            entity.setId(null);
            ///////////////////////////////////////////////////////////////////////

            stragegyService.add(entity);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("添加成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("添加失败" + e.getMessage());
            return orr;
        }
    }

    @ApiOperation(value = "修改", notes = "")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<Stragegy> update(@RequestBody @Validated Stragegy entity,BindingResult results ,HttpServletRequest request) {
        ObjectRestResponse<Stragegy> orr = new ObjectRestResponse<Stragegy>();
        orr.setData(null);
        try {
            ////////////////////此处获取用户ID，获取不到则是未登录////////////////
            User user;
            HttpSession session = request.getSession();
            /////////////////////临时模拟登录////////////////////////////
//            User usertemp=new User();
//            usertemp.setId("1");
//            session.setAttribute("user",usertemp);
            /////////////////////////!!!!!!!!!!//////////////////////////
            user=(User)session.getAttribute("user");
            String userid=null;
            if(!(user==null)){
                userid=user.getId();
            }
            if (userid ==null || userid.equals("")) {
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("用户未登录");
                return orr;
            }
            //////////////////////此处判断修改的文章是否为该用户所有/////////////////
            Stragegy temp =stragegyService.findById(entity.getId());
            if (!userid.equals(temp.getUserId())) {
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("越权操作你是"+userid+"不是"+temp.getUserId());
                return orr;
            }
            //////////////////////////////开始修改//////////////////////////////////
            entity.setUserId(userid);
            entity.setCreateTime(temp.getCreateTime());
            entity.setUpdateTime(new Date());
            stragegyService.update(entity);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("修改成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("修改失败" + e.getMessage());
            return orr;
        }
    }

    @ApiOperation(value = "删除", notes = "")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<Stragegy> delete(@PathVariable String id,HttpServletRequest request) {
        ObjectRestResponse<Stragegy> orr = new ObjectRestResponse<Stragegy>();
        orr.setData(null);
        try {
            ////////////////////此处获取用户ID，获取不到则是未登录////////////////
            User user;
            HttpSession session = request.getSession();
            /////////////////////临时模拟登录////////////////////////////
//            User usertemp=new User();
//            usertemp.setId("1");
//            session.setAttribute("user",usertemp);
            /////////////////////////!!!!!!!!!!//////////////////////////
            user=(User)session.getAttribute("user");
            String userid=null;
            if(!(user==null)){
                userid=user.getId();
            }
            if (userid ==null || userid.equals("")) {
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("用户未登录");
                return orr;
            }
            //////////////////////此处判断修改的文章是否为该用户所有/////////////////
            Stragegy temp =stragegyService.findById(id);
            if (!userid.equals(temp.getUserId())) {
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("越权操作，你是"+userid+"不是"+temp.getUserId());
                return orr;
            }
            //////////////////////////////开始删除//////////////////////////////////
            stragegyService.delete(id);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("删除成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("删除失败" + e.getMessage());
            return orr;
        }
    }
    @ApiOperation(value = "分页查询", notes = "")
    @RequestMapping(value = "/pageQuery", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Stragegy> pageQuery(@ModelAttribute StragegyModel queryModel) {
        TableResultResponse<Stragegy> trr = new TableResultResponse<Stragegy>();

        try {
            List<Stragegy> list = stragegyService.pageQuery(queryModel);
            trr.getData().setTotal(stragegyService.pageQueryCount(queryModel));
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }
    @ApiOperation(value = "查询所有", notes = "")
    @RequestMapping(value = "/findList", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Stragegy> findList(@ModelAttribute StragegyModel queryModel) {
        TableResultResponse<Stragegy> trr = new TableResultResponse<Stragegy>();
        try {
            List<Stragegy> list = stragegyService.findList(queryModel);
            trr.getData().setTotal(list.size());
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }

    @ApiOperation(value = "根据id查询", notes = "")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Stragegy> findById(@PathVariable String id,HttpServletRequest request) {
        ObjectRestResponse<Stragegy> orr = new ObjectRestResponse<Stragegy>();

        try {
            ///////////////////////获取当前登录用户////////////////////////

            User user;
            HttpSession session = request.getSession();
            /////////////////////临时模拟登录////////////////////////////
//            User usertemp=new User();
//            usertemp.setId("1");
//            session.setAttribute("user",usertemp);
            /////////////////////////!!!!!!!!!!//////////////////////////
            user=(User)session.getAttribute("user");
            String userid=null;
            if(!(user==null)){
                userid=user.getId();
            }
            ///////////////////////执行查出攻略////////////////////////
            Stragegy stragegy = stragegyService.findById(id);
            ///////////////////////判断当前用户是否收藏////////////////////////
            if(user ==null)stragegy.setIsCollected(0);
            else {
                StragegyCollectModel temp=new StragegyCollectModel();
                temp.setUserId(userid);
                temp.setStragegyId(id);
                System.out.println(temp);
                int flag=stragegyCollectService.isCollected(temp);
                stragegy.setIsCollected(flag);
            }
            ///////////////////////获取当前文章关注数量////////////////////////
            stragegy.setCollectCount(stragegyCollectService.collected(id));
            ///////////////////////返回数据////////////////////////
            orr.setData(stragegy);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("查询成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("查询失败" + e.getMessage());
            return orr;
        }
    }

    @ApiOperation(value = "根据用户ID查询", notes = "")
    @RequestMapping(value = "/findByUserid", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Stragegy> findByUserid(String userid) {
        TableResultResponse<Stragegy> trr = new TableResultResponse<Stragegy>();
        try {
            List<Stragegy> list = stragegyService.findByUserId(userid);
            trr.getData().setTotal(list.size());
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }
}
